<?php
/**
 * +----------------------------------------------------------------------
 * | Copyright © Sogu Technologies Co. Ltd 2018-2050 All rights reserved.
 * +----------------------------------------------------------------------
 * | File name: RepaymentController.php
 * +----------------------------------------------------------------------
 * | Author: lijiafei
 * +----------------------------------------------------------------------
 * | Description:提前结清
 * +----------------------------------------------------------------------
 * | Others: 其他内容的说明
 * +----------------------------------------------------------------------
 * | History:
 * +----------------------------------------------------------------------
 * | 1.Date: 2019/3/1 Author: lijiafei Modification: 创建
 * +----------------------------------------------------------------------
 */

namespace App\Http\Controllers\Collection;


use App\Models\CollectionApiRequest;
use App\Models\RepayRecord;
use App\Models\RepaySummary;
use App\Models\UserBankCard;
use Illuminate\Support\Carbon;

class RepaymentController extends BaseController
{
    public function repayment(){
        $data = $this->pubData;
        //判断是否可以全部还清
        $loan_id = $data['caseCode'];
        $summaryInfo = RepaySummary::select(['uid','status'])->where('loan_id',$loan_id)->first();
        if(!$summaryInfo){
            $summaryInfo = RepaySummary::select(['uid','status'])->where('trans_id',$loan_id)->first();
        }


        if(!$summaryInfo){
            $returnData['result'] = CollectionApiRequest::RESPONSE_OTHER_ERROR;
            $returnData['msg'] = '用户数据不存在';
            return collectionResp($returnData);
        }
        if($summaryInfo->status == RepaySummary::STATUS_TWO){
            $returnData['result'] = CollectionApiRequest::RESPONSE_OTHER_ERROR;
            $returnData['msg'] = '当前还款数据已还清';
            return collectionResp($returnData);
        }
        //判断是否有逾期
        $summaryOneInfo = RepaySummary::getRepaySummaryLog($summaryInfo->uid);
        $result = $this->verifyData($summaryOneInfo);
        if($result['result'] == CollectionApiRequest::RESPONSE_OTHER_ERROR){
            return collectionResp($result);
        }
        //获取全部还清的时候的记录 period = 0的记录
        $info = RepaySummary::getAllRepayLog($summaryOneInfo->id,$summaryOneInfo->uid);
        if(!$info){
            $returnData['result'] = CollectionApiRequest::RESPONSE_OTHER_ERROR;
            $returnData['msg'] = '提前结清失败';
            return collectionResp($returnData);
        }
        if($info->recordOne->status == RepayRecord::STATUS_TWO){
            $returnData['result'] = CollectionApiRequest::RESPONSE_OTHER_ERROR;
            $returnData['msg'] = '正在还款中,不能重复发起';
            return collectionResp($returnData);
        }
        $isSave = RepaySummary::saveBankInfo($summaryOneInfo->recordOne->bank_cards_id,$summaryOneInfo->uid);
        if($isSave === false){
            $returnData['result'] = CollectionApiRequest::RESPONSE_OTHER_ERROR;
            $returnData['msg'] = '还款失败,请稍后重试';
            return collectionResp($returnData);
        }

        $flag = RepaySummary::checkApi($info->recordOne->summary_id,$info->recordOne->uid);
        if(!$flag){
            $returnData['result'] = CollectionApiRequest::RESPONSE_OTHER_ERROR;
            $returnData['msg'] = '当日请求次数已达上限';
            return collectionResp($returnData);
        }
        $result = RepaySummary::setAllRepayJob($info->recordOne,RepayRecord::REPAY_TYPE_REPAYMENT);
        if($result['flag'] == true){
            $returnData['result'] = CollectionApiRequest::RESPONSE_OK;
        }else{
            $returnData['result'] = CollectionApiRequest::RESPONSE_OTHER_ERROR;
        }
        $returnData['msg'] = $result['msg'];
        return collectionResp($returnData);

    }


    protected function verifyData(RepaySummary $summaryInfo){
        //判断是否有逾期的记录
        $day_number = count_days(Carbon::now()->toDateString(),$summaryInfo->recordOne->normal_date);
        if($day_number > 0){
            $returnData['result'] = CollectionApiRequest::RESPONSE_OTHER_ERROR;
            $returnData['msg'] = '当前还款记录有逾期记录,不能全部结清';
            return $returnData;
        }
        //判断当前银行卡是否有效
        $hold_agreement_num = UserBankCard::getHoldAgreementNumById($summaryInfo->recordOne->bank_cards_id,$summaryInfo->id);
        if(!$hold_agreement_num){
            $returnData['result'] = CollectionApiRequest::RESPONSE_OTHER_ERROR;
            $returnData['msg'] = '用户的银行卡协议号有误';
            return $returnData;
        }
        if($summaryInfo === false){
            $returnData['result'] = CollectionApiRequest::RESPONSE_OTHER_ERROR;
            $returnData['msg'] = '未查询到还款记录';
            return $returnData;
        }
        if($summaryInfo->recordOne->status == RepayRecord::STATUS_TWO){
            //代表这条记录还在轮询中,不要还款
            $returnData['result'] = CollectionApiRequest::RESPONSE_OTHER_ERROR;
            $returnData['msg'] = '正在还款中,不能全部结清';
            return $returnData;
        }
        //判断是否可以全部还清
        $isAllRepay = RepaySummary::isAllRepay($summaryInfo->recordOne);
        if(!$isAllRepay){
            $returnData['result'] = CollectionApiRequest::RESPONSE_OTHER_ERROR;
            $returnData['msg'] = '当前状态不能进行提前结清';
            return $returnData;
        }
        return ['result' => CollectionApiRequest::RESPONSE_OK];
    }

}